package com.htzos.common.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 从前台转义
 * @author Administrator
 *
 */
public class HtmlUtil {

	/**
	 * 将textarea传入的值转化成html代码
	 * @param text
	 * @return
	 */
	public static String encode(String text){
		if(text==null)
			return null;
		
		text = text.replaceAll(">", "&gt;");
		text = text.replaceAll("<", "&lt;");
		text = text.replaceAll("\n", "<br/>");
		
		return text;
	}
	
	
	
	 private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式  
	    private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式  
	    private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式  
	    private static final String regEx_space = "\\s*|\t|\r|\n";//定义空格回车换行符  
	      
	    /** 
	     * @param htmlStr 
	     * @return 
	     *  删除Html标签 
	     */  
	    public static String delHTMLTag(String htmlStr) {  
	        Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);  
	        Matcher m_script = p_script.matcher(htmlStr);  
	        htmlStr = m_script.replaceAll(""); // 过滤script标签  
	  
	        Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);  
	        Matcher m_style = p_style.matcher(htmlStr);  
	        htmlStr = m_style.replaceAll(""); // 过滤style标签  
	  
	        Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);  
	        Matcher m_html = p_html.matcher(htmlStr);  
	        htmlStr = m_html.replaceAll(""); // 过滤html标签  
	  
	        Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);  
	        Matcher m_space = p_space.matcher(htmlStr);  
	        htmlStr = m_space.replaceAll(""); // 过滤空格回车标签  
	        return htmlStr.trim(); // 返回文本字符串  
	    }  
	      
	    public static String getTextFromHtml(String htmlStr){  
	        htmlStr = delHTMLTag(htmlStr);  
	        htmlStr = htmlStr.replaceAll("&nbsp;", "");  
	        htmlStr = htmlStr.replaceAll("@@", "");  
	        //htmlStr = htmlStr.substring(0, htmlStr.indexOf("。")+1);  //本行为wcg在调试时发现，若传入字符串内不含“。”时，字符串截取后为空
	        return htmlStr;  
	    }  
	    public static String getFirstImgFromHtml(String htmlStr){
	    	 String regexImg = "<img.+?src\\s*=\\s*['|\"]?\\s*([^'\"\\s>]+).+?/?>?";     
	         String ImageStr="";  
	         String ImageSrcStr="";  
	         Pattern p = Pattern.compile(regexImg,Pattern.CASE_INSENSITIVE);  
	         Matcher m = p.matcher(htmlStr);  
	         if(m.find()){  
	             ImageStr = m.group();  
	             ImageSrcStr = m.group(1);  
	         }  
	         return ImageSrcStr;
	    }  
	    public static void main(String[] args) {  
	        String str = "<div style='text-align:center;'> 整治“四风”   清弊除垢<br/><img src= \"aa.jpg\"/><img src=bb.jpg /> <img src='cc.jpg' /> <img src=\"dd.jpg\"/><span style='font-size:14px;'> </span><span style='font-size:18px;'>公司召开党的群众路线教育实践活动动员大会</span><br/></div>";  
	        System.out.println(getTextFromHtml(str));  
	    }  
}
